/*
 * @Description:
 * @Author: zhougang
 * @Date: 2025-07-14 15:46:46
 * @LastEditors: zhougang
 * @LastEditTime: 2025-07-15 15:15:00
 */
import eslint from "@eslint/js";
import vuePlugin from "eslint-plugin-vue";
import tsPlugin from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
import globals from "globals";
import vueParser from "vue-eslint-parser";
import prettierPlugin from "eslint-plugin-prettier";
import prettierConfig from "eslint-config-prettier";
// import { env } from "process";
// import eslintrcImportJson from "./.eslintrc-auto-import.json" assert { type: "json" };

// 解决eslint检查报错 needs an import attribute of "type: json"
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const eslintrcImportJson = JSON.parse(
  readFileSync(join(__dirname, ".eslintrc-auto-import.json"), "utf8")
);

// console.log("------prettierConfig.rules", prettierConfig.rules);

export default [
  {
    ignores: [
      "**/node_modules/**",
      "**/*.js",
      "**/dist/**",
      "**/build/**",
      "**/coverage/**",
      "**/.git/**",
      "**/.vscode/**",
      "**/.idea/**",
      "**/*.min.js",
      "**/package-lock.json",
      "**/yarn.lock",
      "**/pnpm-lock.yaml",
      "**/__snapshots__/**",
      "**/*.d.ts",
      "**/webpack.config.js",
      "**/vite.config.js",
      "**/*.html",
    ],
  },
  { languageOptions: { ...eslintrcImportJson } },

  {
    languageOptions: {
      globals: {
        Api: "readonly", Requests: "readonly",
        tipUtils: "readonly", Custom: "readonly", toolsfunc: "readonly",
        Ep: "readonly"
      },
    },
  },

  eslint.configs.recommended,
  ...vuePlugin.configs["flat/recommended"],
  prettierConfig,

  // 新增：TSX 文件专用配置
  {
    files: ["**/*.tsx"],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        ecmaFeatures: {
          jsx: true, // 启用 JSX 支持
        },
        project: "./tsconfig.app.json",
      },
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2021,
        JSX: "readonly", // 添加 JSX 全局类型
        React: "readonly", // 虽然不用 React，但一些类型可能引用
      },
    },
    plugins: {
      "@typescript-eslint": tsPlugin,
      vue: vuePlugin, // 添加 vue 插件以支持 Vue JSX
    },
    rules: {
      // TSX 特定规则
      "vue/jsx-uses-vars": "error", // 防止 JSX 中使用的变量被标记为未使用
    },
  },

  {
    files: ["**/*.ts", "**/*.vue", "**/*.tsx"],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        // project: "./tsconfig.app.json",
      },
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2021,
      },
    },
    plugins: {
      "@typescript-eslint": tsPlugin,
      // prettier: prettierPlugin,
    },
    rules: {},
  },
  // Vue 专用
  {
    // files: ["**/*.vue", "src/**/*.vue", "src/components/**/*.vue"],
    files: ["**/*.vue"],
    languageOptions: {
      parser: vueParser,
      parserOptions: {
        parser: tsParser,
        extraFileExtensions: [".vue"],
        ecmaVersion: "latest",
        sourceType: "module",
        // project: "./tsconfig.app.json",
      },
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2021,
      },
    },
    plugins: {
      vue: vuePlugin, // 手动注册插件
      prettier: prettierPlugin,
    },
    rules: {
      // 允许单个单词的组件名，或者为特定文件配置例外
      "vue/multi-word-component-names": [
        "error",
        {
          ignores: ["index"],
        },
      ],
      // "vue/component-name-in-template-casing": [
      //   "error",
      //   "kebabcase",
      //   {
      //     // ignores: ["el-button"],
      //   },
      // ],
    },
  },
  // ts 专用 （node环境）
  {
    files: ["src/**/*.ts"],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        project: "./tsconfig.app.json",
      },
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2021,
      },
    },
    plugins: {
      "@typescript-eslint": tsPlugin,
      prettier: prettierPlugin,
    },
    rules: {},
  },
  // ts 专用 （浏览器环境）
  {
    files: ["vite.config.ts", "devProxy.ts"],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        project: "./tsconfig.node.json",
      },
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2021,
      },
    },
    plugins: {
      "@typescript-eslint": tsPlugin,
      prettier: prettierPlugin,
    },
    rules: {},
  },
  // html
  {
    files: ["**/*.html"],
    plugins: {
      prettier: prettierPlugin,
    },
  },
  {
    plugins: {
      "@typescript-eslint": tsPlugin,
    },
    rules: {
      // 添加 TypeScript 特定规则
      "@typescript-eslint/consistent-type-imports": "error",
      "no-unused-vars": "off",
      "@typescript-eslint/no-unused-vars": [
        "warn", // 保持 warn 级别
        {
          argsIgnorePattern: "^_", // 忽略以 _ 开头的参数
          varsIgnorePattern: "^_", // 可选：忽略以 _ 开头的变量
          caughtErrorsIgnorePattern: "^_", // 可选：忽略以 _ 开头的 catch 参数
        },
      ],

      "vue/no-deprecated-slot-attribute": "error",
      "vue/no-deprecated-v-on-native-modifier": "error",
      "vue/no-deprecated-v-bind-sync": "error",
      // "@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
      // 添加prettier规则，将格式问题报告为错误
      "prettier/prettier": "error",
      ...prettierConfig.rules,
    },
  },
];
